package com.lzh.others.basejava.Thread_21.bilibili_course_thread.CompletableFuturePrac_07;

import com.lzh.others.basejava.Thread_21.utils.SmallTool;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * 自定义线程池
 *
 * @author LiuZiHao
 * @date 创建时间:2023年05月22日 11:17:48
 */
public class CompletableFuturePrac_07_customThreadPoll {
    public static void main(String[] args) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < 24; i++) {
            Random random = new Random();
            int i1 = random.nextInt(10);
            result.append(i1);
        }
        System.out.println(result.toString());
        ExecutorService threadPool = Executors.newCachedThreadPool();

        SmallTool.printTimeAndThread("小白和小伙伴们 进餐厅点菜了");
        long startTime = System.currentTimeMillis();

        CompletableFuture[] dishes = IntStream.rangeClosed(1, 7)
                .mapToObj(i -> new Dish("菜" + i, 1))
                .map(dish -> CompletableFuture.runAsync(dish::makeUseCpu, threadPool))
                .toArray(CompletableFuture[]::new);

        CompletableFuture.allOf(dishes).join();

        threadPool.shutdown();

        SmallTool.printTimeAndThread("菜都做好了,上桌" + (System.currentTimeMillis() - startTime));

    }
}
